/*
 * Table.java
 *
 * Created on 7 de Novembro de 2008, 19:21
 */

package br.com.jpoker.common.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import br.com.jpoker.common.ui.components.JGradientPanel;
import br.com.jpoker.common.ui.layout.AbsoluteConstraints;
import br.com.jpoker.common.ui.layout.AbsoluteLayout;
import br.com.jpoker.engine.Engine;
import br.com.jpoker.model.Player;
import br.com.jpoker.model.Table;
import br.com.jpoker.resources.images.ImageHelper;

/**
 *
 * @author  bernardes
 */
public class PokerTableFrame extends JFrame {
    
	private static final Image APP_ICON = ImageHelper.loadImage("appicon.png").getImage();
	private static final ImageIcon CARD_BORDER = ImageHelper.loadImage("cards/card-border.gif");
	private Table table;
	private static Font font = Font.decode("arial 18");
	
    private JPanel jPanel1;
  
    
    /** Creates new form Table */
    public PokerTableFrame(Table table) {
    	this.table = table;
        initGui();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initGui() {
    	
    	setTitle("JHoldem");
    	setIconImage(APP_ICON);
        jPanel1 = new JPanel();
        JLabel p1fc = table.getPlaceHolderCards()[0];
        JLabel p2fc = table.getPlaceHolderCards()[1];
        JLabel p3fc = table.getPlaceHolderCards()[2];
        JLabel p4fc = table.getPlaceHolderCards()[3];
        JLabel p5fc = table.getPlaceHolderCards()[4];
        JLabel p6fc = table.getPlaceHolderCards()[5];
        JLabel p7fc = table.getPlaceHolderCards()[6];
        JLabel p8fc = table.getPlaceHolderCards()[7];
        
        JLabel p1sc = table.getPlaceHolderCards()[8];
        JLabel p2sc = table.getPlaceHolderCards()[9];
        JLabel p3sc = table.getPlaceHolderCards()[10];
        JLabel p4sc = table.getPlaceHolderCards()[11];
        JLabel p5sc = table.getPlaceHolderCards()[12];
        JLabel p6sc = table.getPlaceHolderCards()[13];
        JLabel p7sc = table.getPlaceHolderCards()[14];
        JLabel p8sc = table.getPlaceHolderCards()[15];

        
        List<Integer> ocuppiedPlaces = table.getOcuppiedPlaces();
        JPanel panel = new JGradientPanel(Color.GREEN.darker().darker(), Color.GREEN.darker().darker().darker(), 300);
        JPanel mainPanel = new JPanel(new AbsoluteLayout());
        
        JPanel flopPanel = new JPanel(new AbsoluteLayout());

        JLabel flop1 = table.getFlopHolderCards()[0];
        flop1.setIcon(CARD_BORDER);
        JLabel flop2 = table.getFlopHolderCards()[1];
        flop2.setIcon(CARD_BORDER);
        JLabel flop3 = table.getFlopHolderCards()[2];
        flop3.setIcon(CARD_BORDER);
        JLabel flop4 = table.getFlopHolderCards()[3];
        flop4.setIcon(CARD_BORDER);
        JLabel flop5 = table.getFlopHolderCards()[4];
        flop5.setIcon(CARD_BORDER);
        
        flopPanel.add(flop1, new AbsoluteConstraints(50, 60, 75, 100));
        flopPanel.add(flop2, new AbsoluteConstraints(135, 60, 75, 100));
        flopPanel.add(flop3, new AbsoluteConstraints(220, 60, 75, 100));
        flopPanel.add(flop4, new AbsoluteConstraints(305, 60, 75, 100));
        flopPanel.add(flop5, new AbsoluteConstraints(390, 60, 75, 100));
        
        flopPanel.setOpaque(false);
        try {
			panel.setPreferredSize(new java.awt.Dimension(956, 540));
			GridBagLayout thisLayout = new GridBagLayout();
			thisLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.1};
			thisLayout.rowHeights = new int[] {10, 100, 10, 100, 40, 100, 7, 100, 7};
			thisLayout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1};
			thisLayout.columnWidths = new int[] {0, 10, 75, 10, 75, 75, 75, 10, 75, 150, 75, 10, 75, 75, 75, 12, 71, 7};
			panel.setLayout(thisLayout);
			
			jPanel1 = new JPanel();
			BorderLayout jPanel1Layout = new BorderLayout();
			panel.add(jPanel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			jPanel1.setLayout(jPanel1Layout);
			jPanel1.setBackground(new java.awt.Color(255,255,255));
			jPanel1.setEnabled(false);
			
			if(ocuppiedPlaces.contains(Integer.valueOf(1))){
				panel.add(p1fc, new GridBagConstraints(10, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p1fc.setIcon(CARD_BORDER);
				panel.add(p1sc, new GridBagConstraints(12, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p1sc.setIcon(CARD_BORDER);
				JLabel lblPlayer1 = new JLabel(table.getSitPlaces()[0].getPlayer().getName());
				lblPlayer1.setFont(font);
				lblPlayer1.setForeground(Color.WHITE);
				panel.add(lblPlayer1, new GridBagConstraints(12, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			}
			
			
			if(ocuppiedPlaces.contains(Integer.valueOf(2))){
				panel.add(p2fc, new GridBagConstraints(14, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p2fc.setIcon(CARD_BORDER);
				panel.add(p2sc, new GridBagConstraints(16, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p2sc.setIcon(CARD_BORDER);
				JLabel lblPlayer2 = new JLabel(table.getSitPlaces()[1].getPlayer().getName());
				lblPlayer2.setFont(font);
				lblPlayer2.setForeground(Color.WHITE);
				panel.add(lblPlayer2, new GridBagConstraints(16, 4, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			}
			
			if(ocuppiedPlaces.contains(Integer.valueOf(3))){
				panel.add(p3fc, new GridBagConstraints(14, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p3fc.setIcon(CARD_BORDER);
				panel.add(p3sc, new GridBagConstraints(16, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p3sc.setIcon(CARD_BORDER);
				JLabel lblPlayer3 = new JLabel(table.getSitPlaces()[2].getPlayer().getName());
				lblPlayer3.setFont(font);
				lblPlayer3.setForeground(Color.WHITE);
				panel.add(lblPlayer3, new GridBagConstraints(16, 7, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			}
			
			if(ocuppiedPlaces.contains(Integer.valueOf(4))){
				panel.add(p4fc, new GridBagConstraints(10, 7, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p4fc.setIcon(CARD_BORDER);
				panel.add(p4sc, new GridBagConstraints(12, 7, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p4sc.setIcon(CARD_BORDER);
				JLabel lblPlayer4 = new JLabel(table.getSitPlaces()[3].getPlayer().getName());
				lblPlayer4.setFont(font);
				lblPlayer4.setForeground(Color.WHITE);
				panel.add(lblPlayer4, new GridBagConstraints(12, 8, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			}
		
			if(ocuppiedPlaces.contains(Integer.valueOf(5))){
				panel.add(p5fc, new GridBagConstraints(6, 7, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p5fc.setIcon(CARD_BORDER);
				panel.add(p5sc, new GridBagConstraints(8, 7, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p5sc.setIcon(CARD_BORDER);
				JLabel lblPlayer5 = new JLabel(table.getSitPlaces()[4].getPlayer().getName());
				lblPlayer5.setFont(font);
				lblPlayer5.setForeground(Color.WHITE);
				panel.add(lblPlayer5, new GridBagConstraints(8, 8, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			}
			
			if(ocuppiedPlaces.contains(Integer.valueOf(6))){
				panel.add(p6fc, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p6fc.setIcon(CARD_BORDER);
				panel.add(p6sc, new GridBagConstraints(4, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p6sc.setIcon(CARD_BORDER);
				JLabel lblPlayer6 = new JLabel(table.getSitPlaces()[5].getPlayer().getName());
				lblPlayer6.setFont(font);
				lblPlayer6.setForeground(Color.WHITE);
				panel.add(lblPlayer6, new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			}
			
			if(ocuppiedPlaces.contains(Integer.valueOf(7))){
				panel.add(p7fc, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p7fc.setIcon(CARD_BORDER);
				panel.add(p7sc, new GridBagConstraints(4, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p7sc.setIcon(CARD_BORDER);
				JLabel lblPlayer7 = new JLabel(table.getSitPlaces()[6].getPlayer().getName());
				lblPlayer7.setFont(font);
				lblPlayer7.setForeground(Color.WHITE);
				panel.add(lblPlayer7, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			}
			
			if(ocuppiedPlaces.contains(Integer.valueOf(8))){
				panel.add(p8fc, new GridBagConstraints(6, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p8fc.setIcon(CARD_BORDER);
				panel.add(p8sc, new GridBagConstraints(8, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				p8sc.setIcon(CARD_BORDER);
				JLabel lblPlayer8 = new JLabel(table.getSitPlaces()[7].getPlayer().getName());
				lblPlayer8.setFont(font);
				lblPlayer8.setForeground(Color.WHITE);
				panel.add(lblPlayer8, new GridBagConstraints(6, 2, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			}
			
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		setResizable(false);
		mainPanel.add(flopPanel, new AbsoluteConstraints(220,140,-1,-1));
		mainPanel.add(panel, new AbsoluteConstraints(0,0,-1,-1));
		getContentPane().add(mainPanel);
        pack();
    }               
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
    	
    	final Table t = new Table();
    	
		t.addPlayer(new Player("Marlon"), 1);
		t.addPlayer(new Player("Gabriela"), 2);
		t.addPlayer(new Player("Veller"), 5);
		t.addPlayer(new Player("Eskeff"), 6);
		t.addPlayer(new Player("Giuliano"), 3);
      
		java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               m = new PokerTableFrame(t){
				private static final long serialVersionUID = 1L;{
					setVisible(true);
				}
			};
            }
        });

		java.awt.EventQueue.invokeLater(new Runnable() {
        	public void run() {
        		new Engine(t).roundStart();
        	}
        });

		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Engine(t).showFlop();
			}
		});

    }
    
    private static PokerTableFrame m = null;
    public static PokerTableFrame getInstance(){
    	return m;
    }

	public Table getTable() {
		return table;
	}

	public void setTable(Table table) {
		this.table = table;
	}

	
    
}
